package com.example.hibernate.g.collection_compononent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.CollectionId;

@Entity
@Table(name="T_ITEM")
@SequenceGenerator(name="SEQ_ITEM", sequenceName="SEQ_ITEM", allocationSize=1)
@org.hibernate.annotations.Entity(
		dynamicInsert=true,dynamicUpdate=true	
		)
public class Item {
	
	@Id
	@GeneratedValue(generator="SEQ_ITEM")
	private Long id;
	private String name;
	
	@ElementCollection
	@CollectionTable(name="T_IMAGE_SET", joinColumns=@JoinColumn(name="ITEM_ID"))
	@AttributeOverrides({
		@AttributeOverride(name="FILENAME", column=@Column(name="IMAGE_FILENAME")),
		@AttributeOverride(name="SIZEX", column=@Column(name="IMAGE_SIZEX")),
		@AttributeOverride(name="SIZEY", column=@Column(name="IMAGE_SIZEY"))
		})
	private Set<Image> imageSet = new HashSet<Image>();
	
	
	@ElementCollection
	@CollectionTable(name="T_IMAGE_BAG", joinColumns=@JoinColumn(name="ITEM_ID"))
	@AttributeOverride(name="FILENAME", column=@Column(name="IMAGE_FILENAME"))
	@CollectionId(
			columns=@Column(name="ITEM_IMAGE_ID"),
			type=@org.hibernate.annotations.Type(type="long"),
			generator="SEQ_ITEM"
		)
	private Collection<Image> imageBag = new ArrayList<Image>();
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<Image> getImageSet() {
		return imageSet;
	}
	public void setImageSet(Set<Image> imageSet) {
		this.imageSet = imageSet;
	}
	public Collection<Image> getImageBag() {
		return imageBag;
	}
	public void setImageBag(Collection<Image> imageBag) {
		this.imageBag = imageBag;
	}
	@Override
	public String toString() {
		return "Item [id=" + id + ", name=" + name + "]";
	}
}
